package com.xyxy.web;

import com.google.gson.Gson;
import com.xyxy.dao.impl.BaseDao;
import com.xyxy.pojo.Book;
import com.xyxy.pojo.Cart;
import com.xyxy.pojo.CartItem;
import com.xyxy.service.BookService;
import com.xyxy.service.impl.BookServiceImpl;
import com.xyxy.utils.WebUtils;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class CartServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    //增加购物车商品项
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数,商品编号id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);

        // 调用bookService.queryBookById(id) : Book得到图书的信息
        Book book = bookService.queryBookById(id);

        // 把图书信息，转换成为CartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());

        // 调用Cart.addItem(CartItem)方法添加商品项,购物车只有一个
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if( cart == null ){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        // 重定向回原来商品所在的地址页面
        resp.sendRedirect(req.getHeader("Referer"));

        //最后一个添加到购物车的商品名称
        req.getSession().setAttribute("lastName",cartItem.getName());
    }

    //删除购物车商品项
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart != null){
            cart.deleteItem(id);
        }
        // 重定向回原来购物车展示页面
        resp.sendRedirect(req.getHeader("Referer"));
    }

    //清空购物车
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();

            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    //修改商品数量
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数 商品编号 、商品数量
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        int count = WebUtils.parseInt(req.getParameter("count"), 1);

        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart != null){
            cart.updateItem(id,count);

            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    //用ajax请求来更改首页的提示信息
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取请求的参数,商品编号id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);

        // 调用bookService.queryBookById(id) : Book得到图书的信息
        Book book = bookService.queryBookById(id);

        // 把图书信息，转换成为CartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());

        // 调用Cart.addItem(CartItem)方法添加商品项,购物车只有一个
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if( cart == null ){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        //最后一个添加到购物车的商品名称
        req.getSession().setAttribute("lastName",cartItem.getName());

        //6、返回购物车总的商品数量和最后一个添加的商品名称
        HashMap<String, Object> resultHashMap = new HashMap<>();
        resultHashMap.put("totalCount",cart.getTotalCount());
        resultHashMap.put("lastName",cartItem.getName());

        Gson gson = new Gson();
        String resultHashMapToJson = gson.toJson(resultHashMap);
        resp.getWriter().write(resultHashMapToJson);

    }
}
